/*
 * @(#)Debug.java 1.0 2000/12/18
 */

package tb.test;

import java.io.*;
import junit.framework.*;

/**
 * Debugging support utility.
 * 
 * @author Thomas Barnekow
 * @version 1.0
 */
public class Debug {

	/**
	 * Flag signaling the DEBUG mode.
	 */
	public static final boolean DEBUG = false;

	/**
	 * Our output PrintWriter
	 */
	private static PrintWriter m_out = new PrintWriter(System.err, true);

	/**
	 * Utility class
	 */
	private Debug() {
	}

	/*
	 * Properties
	 */

	public static final void setPrintWriter(PrintWriter out) {
		m_out = out;
	}

	public static final PrintWriter getPrintWriter() {
		return m_out;
	}

	/*
	 * Assertions
	 */

	/*
	 * Asserts that a condition is true. If it isn't it throws an
	 * AssertionFailedError.
	 */
	public static final void assertTrue(boolean condition) {
		if (DEBUG && !condition)
			throw new AssertionFailedError();
	}

	/**
	 * Asserts that a condition is true. If it isn't it throws an
	 * AssertionFailedError with the given message.
	 */
	public static final void assertTrue(String message, boolean condition) {
		if (DEBUG && !condition)
			throw new AssertionFailedError(message);
	}

	/**
	 * Asserts that two objects are equal. If they are not an
	 * AssertionFailedError is thrown.
	 * 
	 * @param expected
	 *            the expected value of an object
	 * @param actual
	 *            the actual value of an object
	 */
	public static final void assertEquals(Object expected, Object actual) {
		if (DEBUG && !expected.equals(actual))
			assertTrue(notEqualsMessage("", expected, actual), false);
	}

	/**
	 * Asserts that two longs are equal.
	 * 
	 * @param expected
	 *            the expected value of an object
	 * @param actual
	 *            the actual value of an object
	 */
	public static final void assertEquals(long expected, long actual) {
		if (DEBUG && expected != actual)
			assertTrue(
				notEqualsMessage("", new Long(expected), new Long(actual)),
				false);
	}

	/**
	 * Asserts that two doubles are equal.
	 * 
	 * @param expected
	 *            the expected value of an object
	 * @param actual
	 *            the actual value of an object
	 * @param delta
	 *            tolerated delta
	 */
	public static final void assertEquals(
		double expected,
		double actual,
		double delta) {
		if (DEBUG && Math.abs(expected - actual) > delta)
			assertTrue(
				notEqualsMessage("", new Double(expected), new Double(actual)),
				false);
	}

	/**
	 * Asserts that two objects are equal. If they are not an
	 * AssertionFailedError is thrown.
	 * 
	 * @param message
	 *            the detail message for this assertion
	 * @param expected
	 *            the expected value of an object
	 * @param actual
	 *            the actual value of an object
	 */
	public static final void assertEquals(
		String message,
		Object expected,
		Object actual) {
		if (DEBUG && !expected.equals(actual))
			assertTrue(
				notEqualsMessage(message + " ", expected, actual),
				false);
	}

	/**
	 * Asserts that two longs are equal.
	 * 
	 * @param message
	 *            the detail message for this assertion
	 * @param expected
	 *            the expected value of an object
	 * @param actual
	 *            the actual value of an object
	 */
	public static final void assertEquals(
		String message,
		long expected,
		long actual) {
		if (DEBUG && expected != actual)
			assertTrue(
				notEqualsMessage(
					message + " ",
					new Long(expected),
					new Long(actual)),
				false);
	}

	/**
	 * Asserts that two doubles are equal.
	 * 
	 * @param message
	 *            the detail message for this assertion
	 * @param expected
	 *            the expected value of an object
	 * @param actual
	 *            the actual value of an object
	 * @param delta
	 *            tolerated delta
	 */
	public static final void assertEquals(
		String message,
		double expected,
		double actual,
		double delta) {
		if (DEBUG && Math.abs(expected - actual) > delta)
			assertTrue(
				notEqualsMessage(
					message + " ",
					new Double(expected),
					new Double(actual)),
				false);
	}

	protected static final String notEqualsMessage(
		String message,
		Object expected,
		Object actual) {
		return message
			+ "expected: \""
			+ expected
			+ "\" but was: \""
			+ actual
			+ "\"";
	}

	/*
	 * PrintWriter methods
	 */

	public static final void print(Object o) {
		if (DEBUG)
			m_out.print(o);
	}

	public static final void print(String s) {
		if (DEBUG)
			m_out.print(s);
	}

	public static final void print(int n) {
		if (DEBUG)
			m_out.print(n);
	}

	public static final void print(long n) {
		if (DEBUG)
			m_out.print(n);
	}

	public static final void print(short n) {
		if (DEBUG)
			m_out.print(n);
	}

	public static final void print(float f) {
		if (DEBUG)
			m_out.print(f);
	}

	public static final void print(double d) {
		if (DEBUG)
			m_out.print(d);
	}

	public static final void print(boolean b) {
		if (DEBUG)
			m_out.print(b);
	}

	public static final void print(char c) {
		if (DEBUG)
			m_out.print(c);
	}

	public static final void print(char[] c) {
		if (DEBUG)
			m_out.print(c);
	}

	public static final void println(Object o) {
		if (DEBUG)
			m_out.println(o);
	}

	public static final void println(String s) {
		if (DEBUG)
			m_out.println(s);
	}

	public static final void println(int n) {
		if (DEBUG)
			m_out.println(n);
	}

	public static final void println(long n) {
		if (DEBUG)
			m_out.println(n);
	}

	public static final void println(short n) {
		if (DEBUG)
			m_out.println(n);
	}

	public static final void println(float f) {
		if (DEBUG)
			m_out.println(f);
	}

	public static final void println(double d) {
		if (DEBUG)
			m_out.println(d);
	}

	public static final void println(boolean b) {
		if (DEBUG)
			m_out.println(b);
	}

	public static final void println(char c) {
		if (DEBUG)
			m_out.println(c);
	}

	public static final void println(char[] c) {
		if (DEBUG)
			m_out.println(c);
	}

	public static final void println() {
		if (DEBUG)
			m_out.println();
	}
}
